package com.xgkj.diyiketang.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.RecommPicAdapter;
import com.xgkj.diyiketang.activity.data.VideoAdapter;
import com.xgkj.diyiketang.data.AdvertiseInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.AdvertisementTask;
import com.xgkj.diyiketang.task.SearchVideoTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class ChooseRecommImgActivity extends BaseActivity {
    private int CoverPictureTask = -1;
    private List<AdvertiseInfo> picList = new ArrayList<AdvertiseInfo>();
    private PullToRefreshGridView mListView;
    private int currentPage = 1;
    private NoDataUtil mNoData;
    private RecommPicAdapter mAdapter;
    private TextView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_recomm_img);
        initView();
        doSearch();
    }

    private void initView() {
        mListView = (PullToRefreshGridView) findViewById(R.id.recommend_pic_list);
        mAdapter = new RecommPicAdapter(mContext);
        mListView.setAdapter(mAdapter);
        back = ((TextView) findViewById(R.id.back));
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                currentPage = 1;
                doSearch();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                doSearch();

            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                if (picList != null) {
                    intent.putExtra("iamgeUrl", picList.get(position).getImageUrl());
                    intent.putExtra("Url", picList.get(position).getUrl());
                    setResult(2, intent);
                    finish();
                }
            }
        });
        RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.content_layout);
        mNoData = new NoDataUtil(mContext, contentLayout);
    }

    private void doSearch() {
        if (currentPage == 0) {
            mListView.onRefreshComplete();
            mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            //  ToastUtil.showMessage(mContext,R.string.no_more);
            return;
        }
        AdvertisementTask task = new AdvertisementTask("5", "7");
        task.addListener(uploadImageListener);
        CoverPictureTask = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener uploadImageListener = new ShowToastTaskEventListener(this) {


        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == CoverPictureTask) {
                CoverPictureTask = -1;
                mListView.onRefreshComplete();
                if (data != null) {
                    picList = (List<AdvertiseInfo>) data;
                    if (picList != null && picList.size() > 0) {
                        Log.e("piclist", ": " + picList.get(0).getUrl() + "        " + picList.get(0).getImageUrl());
                    }
                }
                if (currentPage == 1) {
                    mAdapter.clearData();
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }
                if (picList != null && picList.size() > 0) {
                    mAdapter.updateData(picList);
                }

                currentPage = result;
                if (mAdapter != null && mAdapter.getCount() == 0) {
                    mNoData.GridViewEmpty(mListView, null, null, -1);
                } else {
                    mNoData.GridViewNoEmpty(mListView);
                }

            }

        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == CoverPictureTask) {
                CoverPictureTask = -1;
                mListView.onRefreshComplete();
            }
        }
    };
}
